Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

279
Visualizações
right shift >> turns value into zero javascript

Trying some bit manipulation in javascript.

Consider the following:

const n = 4393751543811;
console.log(n.toString(2)) // '111111111100000000000000000000000000000011'
console.log(n & 0b11) // last two bits equal 3
const m = n >> 2; // right shift 2
// The unexpected.
console.log(m.toString(2)) // '0'

The result is 0? The expected output I am looking for after the right shift is:

111111111100000000000000000000000000000011 // pre
001111111111000000000000000000000000000000 // post >> 

How is this accomplished?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Javascript bitwise operators on numbers work "as if" on 32bit integers.

>> (sign-propagating right-shift for numbers) will first convert to a 32-bit integer. If you read linked spec, note specifically

Let int32bit be int modulo 232.

In other words, all bits above 32 will simply be ignored. For your number, this results in the following:

111111111100000000000000000000000000000011
┗removed━┛┗━━━━━━━━━━━━━━32bit━━━━━━━━━━━━━┛

If you want, you can use BigInt:

const n = 4393751543811n; // note the n-suffix
console.log(n.toString(2))
console.log(n & 0b11n) // for BigInt, all operands must be BigInt
const m = n >> 2n;
// The expected.
console.log(m.toString(2))

The spec for >> on BigInt uses BigInt::leftShift(x, -y), where it in turn states:

Semantics here should be equivalent to a bitwise shift, treating the BigInt as an infinite length string of binary two's complement digits.

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda